﻿using System;
using System.Linq;
using System.Web.Mvc;
using TestFileSystem.Models.Directory;

namespace TestFileSystem.Controllers
{
    public class DirectoryController : Controller
    {
        private readonly DirectoryRepository _selectionRepository = new DirectoryRepository();

        public JsonResult GetList(string root)
        {
            int? parentId = (root.Equals("source") ? null : (int?) Convert.ToInt32(root));
            IQueryable<dynamic> directories = (from dir in _selectionRepository.GetAllObjects()
                                                      where dir.ParentDirectoryId.Equals(parentId)
                                                      orderby dir.Name
                                                      select new {
                                                                     id = dir.Id,
                                                                     text = dir.Name,
                                                                     classes = "folder",
                                                                     hasChildren = dir.SubDirectories.Count() > 0
                                                                 });
            return Json(directories);
        }
    }
}
